package com.nox.trademe.testapp.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.nox.trademe.testapp.R;
import com.nox.trademe.testapp.adapters.CustomExpandableAdapter;
import com.nox.trademe.testapp.models.Subcategory;
import com.nox.trademe.testapp.utils.KeyContainer;


public class ExpandableListFragment extends Fragment {

    public static final String TAG = "ExpandableListFragment";
    private Subcategory subcategory;

    public ExpandableListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.v(TAG, "Activity State: onCreateView()");
        View view = inflater.inflate(R.layout.fragment_expandablelist, container, false);

        Bundle args = getArguments();
        if (args == null) {
            subcategory = null;
            Log.v(TAG, "Category NOT received in bundle");
            return view;
        }
        subcategory = (Subcategory)args.getParcelable(KeyContainer.extandableViewKeyBundle);
        Log.v(TAG, "Category received in bundle");

        if (subcategory.getSubcategories().size() > 0) {
            ExpandableListView expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListView);
            expandableListView.setIndicatorBounds(5, 5);
            CustomExpandableAdapter adapter = new CustomExpandableAdapter(subcategory.getSubcategories(), getActivity());
            expandableListView.setAdapter(adapter);
        }
        else {
            TextView textView = (TextView) view.findViewById(R.id.expandableListEmpty);
            textView.setText(R.string.empty_category);
            textView.setText(textView.getText() + subcategory.getCount() + " " + getActivity().getString(R.string.count_phrase));
        }

        return view;
    }

}
